package com.example.easyutils.food;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.easyutils.BaseActivity;
import com.example.easyutils.R;
import com.example.easyutils.httputil.HttpGetRequest;
import com.example.easyutils.util.MyRecycleViewAdapter;
import com.example.easyutils.util.MyTypeAdapter;
import com.example.easyutils.watermark.ClearWatermark;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class FoodCook extends BaseActivity {
    private RecyclerView cRecyclerView;
    private FoodRecycleView myRecycleViewAdapter;//适配器
    private LinearLayoutManager mLinearLayoutManager;//布局管理器
    private EditText inputText;
    private static final int SUCCESS_CODE = 1,ERROR_CODE = 2;
    private List mList;
    Handler handler = new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case SUCCESS_CODE:
                        mLinearLayoutManager = new LinearLayoutManager(FoodCook.this,LinearLayoutManager.VERTICAL,false);
                        myRecycleViewAdapter = new FoodRecycleView(mList,FoodCook.this);
                        cRecyclerView.setLayoutManager(mLinearLayoutManager);
                        cRecyclerView.setAdapter(myRecycleViewAdapter);
                    break;
                case ERROR_CODE:
                    Toast.makeText(FoodCook.this,"出错啦",Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cook);
        inputText = (EditText)findViewById(R.id.editText2);
        cRecyclerView = (RecyclerView)findViewById(R.id.cookRecycle);

//        cRecycleView.setLayoutManager(new GridLayoutManager(this, 3));
//        cRecycleView.setAdapter(new MyRecycleViewAdapter(mList,FoodCook.this));
    }

    public void getCookList(View v){
        String fagStr = inputText.getText().toString();
        Log.d("text",fagStr);
        HttpGetRequest.sendOkHttpGetRequst("https://way.jd.com/jisuapi/search?num=20&appkey=d841f29c61d4b1178380910774222055&keyword="+fagStr, new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("ERROR_COOK",e+"");
                handler.sendEmptyMessage(ERROR_CODE);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String dataMap = response.body().string();
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(new TypeToken<Map<String, Map>>(){

                        }.getType(),new MyTypeAdapter()).create();
                Map<String,Map> dataDetail = gson.fromJson(dataMap,new TypeToken<Map<String,Map>>(){

                }.getType());

                Log.d("logtext",dataDetail+"");
                Log.d("logtext1",((Map)dataDetail.get("result").get("result")).get("list")+"");
                mList = new ArrayList();
                mList = (List) ((Map)dataDetail.get("result").get("result")).get("list");
                handler.sendEmptyMessage(SUCCESS_CODE);
            }
        });
    }
}
